<?php
namespace app\admin\controller\station;

use app\admin\model\ChargingStation;
use app\admin\model\CityArea;
use app\admin\model\SystemAdmin;
use app\admin\model\SystemMenu;
use app\admin\BaseController;
use app\admin\model\SystemRole;
use think\facade\Db;
use think\facade\Log;
use think\App;
use think\facade\View;
use think\facade\Request;

class Station extends BaseController
{
    /**
     * 列表页面
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $cityAreaModel = new CityArea();
        $provinceList =$cityAreaModel->getAllList(['parent_id'=>0],'id,name');
        View::assign('provinceList', $provinceList);
        return View::fetch('station/index');
    }
    public function index2()
    {

        return View::fetch('station/index2');
    }

    public function tixian()
    {

        return View::fetch('tixian/index');
    }
    public function lists(){
        // 从数据库中检索数据并按'id'降序排列
        $list = Db::table('jy_community2')->order('id', 'desc')->select()->toArray();

        // 遍历数据并更新状态描述
        for($i=0;$i<count($list);$i++){
            $list[$i]['createTime'] = date("Y-m-d H:i:s", $list[$i]['createTime']); // 输出格式为年-月-日 时:分:秒
            if($list[$i]['status']==0){
                $list[$i]['status'] = '未审核';
            }else{
                $list[$i]['status'] = '已审核';
            }
        }

        $backData['data'] = $list;
        // 设置成功的响应代码和消息
        $backData['code'] = 0;
        $backData['msg'] = '获取成功';


        // 返回JSON格式的响应
        return json($backData); // ThinkPHP的json方法会自动设置Content-Type为application/json
    }
    function edit2Form(){
        $Id = Request::get('id');
        $list = Db::table('jy_community2')->where('id',$Id)->find();;
        View::assign('list', $list);
        View::assign('id', $Id);
        return View::fetch('station/edit2');
    }
    function updateData2(){
        $getData = Request::post();
        $result = Db::table('jy_community2')->where('id',$getData['id'])->update($getData);;
        if($result) return $this->success('修改成功');
        return $this->fail('未作任何修改');
    }

    /**
     * 获取列表
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list(){
        [$page,$limit] = $this->getPage();
        $getData = Request::post();
        $ChargingStationModel = new ChargingStation();
        $backData = $ChargingStationModel->getList($getData,$page,$limit);
        $backData['code'] = 0;
        $backData['msg'] = '获取成功';
//        print_r($backData);die;
        return json()->data($backData);
    }

    /**
     * 添加表单
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addForm(){
        return View::fetch('station/add');
    }

    /**
     * 添加站点
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addData(){
        $getData = Request::post();
        if(!$getData['title']) return $this->fail('请输站点名称');
        if(!$getData['address']) return $this->fail('请输入站点地址');
        $chargingStationModel = new ChargingStation();
        $result = $chargingStationModel->addData($getData);
        if($result) return $this->success('添加成功');
        return $this->fail('添加失败');
    }

    /**
     * 编辑站点
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function editForm(){
        $adminId = Request::get('id');
        $chargingStationModel = new ChargingStation();
        $info = $chargingStationModel->getInfoById($adminId);
        $infoStatus = 0;
        if($info) $infoStatus = 1;
        $cityAreaModel = new CityArea();
        $areaList = [
            'province_list'=>$cityAreaModel->getAllList(['parent_id'=>0],'id,name'),
            'city_list'=>[],
            'district_list'=>[],
            'street_list'=>[],
        ];
        if(isset($info['province_id']) && $info['province_id']){
            $areaList['city_list'] = $cityAreaModel->getAllList(['parent_id'=>$info['province_id']],'id,name');
        }
        if(isset($info['city_id']) && $info['city_id']){
            $areaList['district_list'] = $cityAreaModel->getAllList(['parent_id'=>$info['city_id']],'id,name');
        }
        if(isset($info['district_id']) && $info['district_id']){
            $areaList['street_list'] = $cityAreaModel->getAllList(['parent_id'=>$info['district_id']],'id,name');
        }
        $info['json_picture'] = $info['picture'];
        if(isset($info['picture']) && $info['picture']){
            $info['picture'] = json_decode($info['picture'],true);
        }

        View::assign('areaList', $areaList);
        View::assign('info', $info);
        View::assign('infoStatus', $infoStatus);
        return View::fetch('station/edit');
    }

    /**
     * 修改站点
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function updateData(){
        $getData = Request::post();
        if(!$getData['title']) return $this->fail('请输站点名称');
        if(!$getData['address']) return $this->fail('请输入站点地址');
        $chargingStationModel = new ChargingStation();
        $result = $chargingStationModel->editData($getData);
        if($result) return $this->success('修改成功');
        return $this->fail('未作任何修改');
    }

    /**
     * 删除站点
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function deleteData(){
        $id = Request::post('id');
        if(!$id) return $this->fail('请选择要删除的站点');
        $chargingStationModel = new ChargingStation();
        $info = $chargingStationModel->getInfoById($id);
        if(!$info)  return $this->fail('请选择正确的站点');
        $result = $chargingStationModel->delData($id);
        if($result['code'] == 0) return $this->success('删除成功');
        return $this->fail('删除失败');
    }

}


